package problem174;

//174.地下城游戏 (困难)
//https://leetcode.cn/problems/dungeon-game/

class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        int n = dungeon.length;
        int m = dungeon[0].length;
        int[][] dp = new int[n+1][m+1];
        for(int i = 0; i<=n; i++) {
            dp[i][m] = Integer.MAX_VALUE;
        }
        for(int j = 0; j<=m; j++) {
            dp[n][j] = Integer.MAX_VALUE;
        }
        dp[n][m-1] = 1;
        for(int i = n-1; i>=0; i--) {
            for(int j = m-1; j>=0; j--) {
                dp[i][j] = Math.min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j];
                dp[i][j] = Math.max(1, dp[i][j]);
            }
        }
        return dp[0][0];
    }
}